{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# NumPy数值计算\n",
    "## 2.1 创建矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[21 32 43 54]\n"
     ]
    }
   ],
   "source": [
    "# 一维数组\n",
    "import numpy as np\n",
    "data=[21,32,43,54]  \n",
    "arr=np.array(data)  \n",
    "print(arr)   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3]\n",
      " [11 12 13]]\n",
      "int64\n"
     ]
    }
   ],
   "source": [
    "#二维数组\n",
    "data=[[1,2,3],[11,12,13]]  \n",
    "arr=np.array(data)  \n",
    "print(arr)  \n",
    "print(arr.dtype) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1. 1. 1. 1.]\n",
      " [1. 1. 1. 1.]\n",
      " [1. 1. 1. 1.]\n",
      " [1. 1. 1. 1.]\n",
      " [1. 1. 1. 1.]\n",
      " [1. 1. 1. 1.]\n",
      " [1. 1. 1. 1.]\n",
      " [1. 1. 1. 1.]]\n",
      "数组维度为： 2 (8, 4)\n"
     ]
    }
   ],
   "source": [
    "#生成全 1 数组\n",
    "a=np.ones([8,4])  # 8行4列\n",
    "print(a)\n",
    "print(\"数组维度为：\",a.ndim,a.shape)  # a.ndim表示数组的维度数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]\n",
      " [16 17 18 19]\n",
      " [20 21 22 23]\n",
      " [24 25 26 27]\n",
      " [28 29 30 31]]\n"
     ]
    }
   ],
   "source": [
    "arr=np.arange(32).reshape((8,4))  \n",
    "print(arr)    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7 8 9]\n"
     ]
    }
   ],
   "source": [
    "print(np.arange(10))   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1.  1.5 2. ]\n"
     ]
    }
   ],
   "source": [
    "print(np.linspace(1,2,3))  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.01457131 0.1222522  0.04095027 0.32554914]\n",
      " [0.08540443 0.39514556 0.95847676 0.24266083]]\n"
     ]
    }
   ],
   "source": [
    "print(np.random.rand(2,4))  # 生成2行4列的随机数，random为0-1范围"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[6 1 7 8]\n"
     ]
    }
   ],
   "source": [
    "print(np.random.randint(1,10,4))  # 最小值1，最大值10(不包括10)，生成4个随机数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0.59503962 0.41339916]\n",
      " [0.0537385  0.00283465]]\n"
     ]
    }
   ],
   "source": [
    "a=np.random.rand(4).reshape(2,2)  # 生成四个在0-1范围的随机数，并reshape为2行2列\n",
    "print(a) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.2 索引与切片\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2]\n"
     ]
    }
   ],
   "source": [
    "arr=np.arange(10)  \n",
    "print(arr[1:3])   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0  1  2  3  4 12 12 12  8  9]\n"
     ]
    }
   ],
   "source": [
    "arr=np.arange(10)  \n",
    "arr[5:8]=12  \n",
    "print(arr)    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0  1  2  3  4 10 10 10 10 10]\n"
     ]
    }
   ],
   "source": [
    "arr=np.arange(10)  \n",
    "arr[5:]=10  \n",
    "print(arr)   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[   0    1    2    3    4    5 1514    7    8    9]\n"
     ]
    }
   ],
   "source": [
    "arr=np.arange(10)  \n",
    "a=arr[5:8]  # 这里将地址给他，改变了原有地址的值\n",
    "a[1]=1514  \n",
    "print(arr)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "arr: [0 1 2 3 4 5 6 7 8 9]\n",
      "b.copy: [0 1 2 3 4 5 6 7 8 9]\n",
      "after change: [  0   1 100   3   4   5   6   7   8   9]\n",
      "arr: [0 1 2 3 4 5 6 7 8 9]\n"
     ]
    }
   ],
   "source": [
    "arr=np.arange(10)  \n",
    "print(\"arr:\",arr)  \n",
    "b=arr.copy()  \n",
    "print(\"b.copy:\",b)  \n",
    "b[2]=100  \n",
    "print(\"after change:\",b)  \n",
    "print(\"arr:\",arr)   # 复制给一个新的数组，原来数组的值不会改变，这里就不是直接在原来的地址进行修改了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "arr: [0 1 2 3 4 5 6 7 8 9]\n",
      "copy [0 1 2 3 4 5 6 7 8 9]\n",
      "after copy [  0   1 100   3   4   5   6   7   8   9]\n",
      "arr: [  0   1 100   3   4   5   6   7   8   9]\n"
     ]
    }
   ],
   "source": [
    "# 与上一个模块代码，形成对比\n",
    "arr=np.arange(10)  \n",
    "print(\"arr:\",arr)  \n",
    "c=arr  \n",
    "print(\"copy\",c)  \n",
    "c[2]=100  \n",
    "print(\"after copy\",c)  #这里是直接地址的引用，所以修改了c的值，arr的值也会改变\n",
    "print(\"arr:\",arr)   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "arr=np.array([[1,2,3],[4,5,6]])  \n",
    "print(arr[1,1])  \n",
    "print(arr[1][1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n"
     ]
    }
   ],
   "source": [
    "arr=np.arange(1,10).reshape(3,3)  \n",
    "print(arr)\n",
    "print(arr[:2])   # 这里将其看作一维数组处理，则展示的为[0-2)行数据，不包括右边界"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[4 5 6]\n",
      " [7 8 9]]\n"
     ]
    }
   ],
   "source": [
    "arr=np.arange(1,10).reshape(3,3)  \n",
    "print(arr[1:])    # 这里将其看作一维数组处理，则展示的为[1-最终]行数据，包含左边界"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1]]\n"
     ]
    }
   ],
   "source": [
    "arr=np.arange(1,10).reshape(3,3)  \n",
    "print(arr[:1,:1])   # 第一个参数对行处理，第二个参数对列处理"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2 3]\n",
      " [5 6]\n",
      " [8 9]]\n"
     ]
    }
   ],
   "source": [
    "arr=np.arange(1,10).reshape(3,3)  \n",
    "print(arr[:,1:])  "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.3 数组操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 3 6]\n",
      " [1 4 7]\n",
      " [2 5 8]]\n",
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "[[ 54  63  72]\n",
      " [ 66  78  90]\n",
      " [ 78  93 108]]\n"
     ]
    }
   ],
   "source": [
    "# 两个矩阵相乘\n",
    "t=np.arange(9).reshape([3,3]).T  \n",
    "print(t)\n",
    "t1=np.arange(1,10).reshape([3,3])  \n",
    "print(t1)\n",
    "print(t.dot(t1)) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "300\n",
      "[[1 2]\n",
      " [3 4]]\n",
      "[[10 20]\n",
      " [30 40]]\n",
      "[[ 70 100]\n",
      " [150 220]]\n"
     ]
    }
   ],
   "source": [
    "lst1=np.array([1,2,3,4])  \n",
    "lst2=np.array([10,20,30,40])  \n",
    "\n",
    "# 两个一维数据点积的话，对应乘积求和\n",
    "print(np.dot(lst1,lst2))  # 1*10+2*20+3*30+4*40=200\n",
    "\n",
    "# 转化为二维数据，进行乘积  \n",
    "print(lst1.reshape([2,2])) \n",
    "print(lst2.reshape([2,2])) \n",
    "print(np.dot(lst1.reshape([2,2]),lst2.reshape([2,2])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.19808523 0.75801931]\n",
      "[0.65691139 0.31079188]\n",
      "[[[0.14247118 0.13994998]\n",
      "  [0.63779429 0.17876619]]\n",
      "\n",
      " [[0.38334197 0.00370937]\n",
      "  [0.88346912 0.20042006]]]\n"
     ]
    }
   ],
   "source": [
    "# 下面二者等价\n",
    "b=np.random.random([1*2]) # 生成2个随机数的一维数组\n",
    "print(b)  \n",
    "b=np.random.random(2) # 生成2个随机数的一维数组\n",
    "print(b)\n",
    "\n",
    "b=np.random.random([2]*3) # 生成2×2的三维数组\n",
    "print(b)  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[ 0  1  2  3]\n",
      "  [ 4  5  6  7]]\n",
      "\n",
      " [[ 8  9 10 11]\n",
      "  [12 13 14 15]]]\n",
      "[[[ 0  1  2  3]\n",
      "  [ 8  9 10 11]]\n",
      "\n",
      " [[ 4  5  6  7]\n",
      "  [12 13 14 15]]]\n",
      "[[[ 0  8]\n",
      "  [ 4 12]]\n",
      "\n",
      " [[ 1  9]\n",
      "  [ 5 13]]\n",
      "\n",
      " [[ 2 10]\n",
      "  [ 6 14]]\n",
      "\n",
      " [[ 3 11]\n",
      "  [ 7 15]]]\n"
     ]
    }
   ],
   "source": [
    "# 不同维数转置，较难理解\n",
    "c=np.arange(16).reshape([2,2,4])  \n",
    "print(c)\n",
    "d=np.transpose(c,[1,0,2])  \n",
    "e=c.T  \n",
    "print(d)  \n",
    "print(e)   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2]\n",
      " [3 4]]\n",
      "[[5 6]\n",
      " [7 8]]\n",
      "[[1 2 5 6]\n",
      " [3 4 7 8]]\n",
      "[[1 2]\n",
      " [3 4]\n",
      " [5 6]\n",
      " [7 8]]\n"
     ]
    }
   ],
   "source": [
    "a1 = np.array([[1,2],[3,4]])  \n",
    "print(a1)\n",
    "a2 = np.array([[5,6],[7,8]])  \n",
    "print(a2)\n",
    "print(np.hstack([a1,a2]))   # 水平堆叠\n",
    "print(np.vstack([a1,a2]))  # 垂直堆叠"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.4数组运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[20.08553692  2.71828183]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "print(np.exp([3,1]))    # 计算e的 3次方 和 1次方"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-0.13541069  0.33703359 -1.39034717]\n",
      " [ 0.51995365  0.92527669  0.71913133]\n",
      " [ 0.37087497 -0.99044571 -0.92795866]]\n",
      "[[-1  1 -1]\n",
      " [ 1  1  1]\n",
      " [ 1 -1 -1]]\n"
     ]
    }
   ],
   "source": [
    "a=np.random.randn(9).reshape([3,3])  # 生成9个标准正态分布的随机数\n",
    "print(a)  \n",
    "print(np.where(a>0,1,-1))    # 条件运算：大于0的变为1，小于0的变为-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "[ -1  -2  -3  -4  -5  -6  -7  -8  -9 -10]\n"
     ]
    }
   ],
   "source": [
    "# 难点\n",
    "a=np.arange(10)  \n",
    "print(a)  \n",
    "b=a.T  # 一维数组不存在行和列，所以转置之后还是本身\n",
    "print(b)\n",
    "b=np.invert(a)  # 对二进制代码按位取反\n",
    "# 数字 0 的二进制：  00000000  取反后：  11111111  (-1)\n",
    "# 数字 1 的二进制：  00000001  取反后：  11111110  (-2)\n",
    "# 数字 2 的二进制：  00000010  取反后：  11111101  (-3)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3]\n",
      " [ 5  7  9]\n",
      " [12 15 18]]\n",
      "[[ 1  3  6]\n",
      " [ 4  9 15]\n",
      " [ 7 15 24]]\n",
      "[[  1   2   3]\n",
      " [  4  10  18]\n",
      " [ 28  80 162]]\n",
      "[[  1   2   6]\n",
      " [  4  20 120]\n",
      " [  7  56 504]]\n"
     ]
    }
   ],
   "source": [
    "a=np.array([[1,2,3],[4,5,6],[7,8,9]])  \n",
    "print(a.cumsum(0))  # 沿着列累加\n",
    "print(a.cumsum(1))  # 沿着行累加\n",
    "print(a.cumprod(0))   # 沿着列累乘\n",
    "print(a.cumprod(1))    # 沿着行累乘"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[-1.31987021 -0.20585068  1.02940675]\n",
      " [ 1.84164428  0.33543006  0.13296036]\n",
      " [-0.35038481 -1.10022305  1.19616392]\n",
      " [-0.86758748  0.02330639  0.82143745]\n",
      " [-1.20686251  0.49586463  0.03483067]]\n",
      "[[-1.31987021 -0.20585068  1.02940675]\n",
      " [ 0.13296036  0.33543006  1.84164428]\n",
      " [-1.10022305 -0.35038481  1.19616392]\n",
      " [-0.86758748  0.02330639  0.82143745]\n",
      " [-1.20686251  0.03483067  0.49586463]]\n"
     ]
    }
   ],
   "source": [
    "arr=np.random.randn(5,3)  #生成15个符合正太分布的随机数\n",
    "print(arr)\n",
    "arr.sort(1)  #沿着行方向排序\n",
    "print(arr)   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 1 0 0 1 0 1 0 1 1 1 1 1 1 0 1 1 1 0 0 0 0 0 1 0 1 0 0 0 1 0 0 1 0 0 0 0\n",
      " 0 0 1 0 0 0 0 1 1 1 0 1 0 0 0 0 1 0 1 1 0 1 1 0 0 1 0 1 0 1 0 1 0 1 0 0 1\n",
      " 0 1 0 1 1 1 0 1 1 1 0 0 1 1 0 1 0 0 1 1 0 1 1 1 0 1]\n",
      "[ 1  1 -1 -1  1 -1  1 -1  1  1  1  1  1  1 -1  1  1  1 -1 -1 -1 -1 -1  1\n",
      " -1  1 -1 -1 -1  1 -1 -1  1 -1 -1 -1 -1 -1 -1  1 -1 -1 -1 -1  1  1  1 -1\n",
      "  1 -1 -1 -1 -1  1 -1  1  1 -1  1  1 -1 -1  1 -1  1 -1  1 -1  1 -1  1 -1\n",
      " -1  1 -1  1 -1  1  1  1 -1  1  1  1 -1 -1  1  1 -1  1 -1 -1  1  1 -1  1\n",
      "  1  1 -1  1]\n",
      "[ 1  2  1  0  1  0  1  0  1  2  3  4  5  6  5  6  7  8  7  6  5  4  3  4\n",
      "  3  4  3  2  1  2  1  0  1  0 -1 -2 -3 -4 -5 -4 -5 -6 -7 -8 -7 -6 -5 -6\n",
      " -5 -6 -7 -8 -9 -8 -9 -8 -7 -8 -7 -6 -7 -8 -7 -8 -7 -8 -7 -8 -7 -8 -7 -8\n",
      " -9 -8 -9 -8 -9 -8 -7 -6 -7 -6 -5 -4 -5 -6 -5 -4 -5 -4 -5 -6 -5 -4 -5 -4\n",
      " -3 -2 -3 -2]\n"
     ]
    }
   ],
   "source": [
    "nsteps=100\n",
    "draws=np.random.randint(0,2,size=nsteps)  \n",
    "print(draws)\n",
    "steps=np.where(draws>0,1,-1)  \n",
    "print(steps)\n",
    "walk=steps.cumsum()  \n",
    "print(walk)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2.5 线性代数运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "行列式值： 12.999999999999995\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "# 判断行列式是否可逆\n",
    "a1 = np.array([[1,1,1],[3,1,4],[8,1,5]])\n",
    "a2 = np.linalg.det(a1) # 大于0则可逆，判断矩阵是否可逆\n",
    "print('行列式值：',a2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "逆矩阵：\n",
      " [[ 0.66666667  0.22222222 -0.11111111]\n",
      " [-0.33333333 -0.16666667  0.16666667]\n",
      " [-0.33333333  0.11111111  0.11111111]]\n",
      "逆矩阵验证：\n",
      " [[ 1.00000000e+00 -6.93889390e-17 -1.38777878e-17]\n",
      " [ 0.00000000e+00  1.00000000e+00 -5.55111512e-17]\n",
      " [-1.11022302e-16 -8.32667268e-17  1.00000000e+00]]\n"
     ]
    }
   ],
   "source": [
    "b1= np.array([[2,2,-1],[1,-2,4],[5,8,2]])\n",
    "b2 = np.linalg.inv(b1) # 获取b1的逆矩阵\n",
    "print('逆矩阵：\\n',b2)\n",
    "print('逆矩阵验证：\\n',np.dot(b1,b2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "线性方程组求解：\n",
      " [[ 3.  2.]\n",
      " [-2. -3.]\n",
      " [ 1.  3.]]\n",
      "线性方程组求解验证：\n",
      " [[2. 5.]\n",
      " [3. 1.]\n",
      " [4. 3.]]\n"
     ]
    }
   ],
   "source": [
    "# 暂时跳，回来再看 \n",
    "A = np.array([[1,2,3],[2,2,1],[3,4,3]])\n",
    "B = np.array([[2,5],[3,1],[4,3]])\n",
    "X = np.linalg.solve(A,B)\n",
    "print('线性方程组求解：\\n',X)\n",
    "print('线性方程组求解验证：\\n',np.dot(A,X)) # A*X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "特征值：\n",
      " [1.99999998 2.00000002 1.        ]\n",
      "特征向量：\n",
      " [[ 7.07106781e-01 -7.07106781e-01 -7.69429096e-17]\n",
      " [ 7.07106781e-01 -7.07106781e-01 -7.07106781e-01]\n",
      " [-1.53464615e-08 -1.53464607e-08 -7.07106781e-01]]\n"
     ]
    }
   ],
   "source": [
    "c = np.array([[3,-1,1],[2,0,1],[1,-1,2]])\n",
    "e,v = np.linalg.eig(c) # 计算特征值和特征向量\n",
    "print('特征值：\\n',e)\n",
    "print('特征向量：\\n',v)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "python39",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.21"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
